							
** ---------------------------------- **							
* Author: Abdulrazzak Tamim 							
* First version: 16/9/2019							
* Last modified: 26/4/2024							
** ---------------------------------- **							
					
* User must define a global macro in order to run the program:												
* "path" contains the raw data												
												
												
clear all												
set more off												
program drop _all												
												
if "`c(username)'"=="abdulrazzaktamim" {												
gl path "/Users/abdulrazzaktamim/Library/CloudStorage/Dropbox/SoilDoc-Abdulrazzak"											
}
								
program main	
								
	Table1
	attrition_2016
	attrition_2019
	balance_reg_outcomes 
	balance_reg_covariates
	
end


program Table1 

	use "$path/replication/data/attrition_balance1.dta", clear
	keep trt attrition_2016 respondent_id
	
	merge 1:1 res using "$path/replication/data/attrition_balance2.dta", nogen
	keep attrition_2019 trt attrition_2016 respondent_id attrition_2015
	
	ta trt
	ta trt attrition_2015
	ta trt attrition_2016
	ta trt attrition_2019
	
	forv i=1/5 { 
		count if trt==`i'
		loc N`i'_2014 = r(N)
		count if trt==`i' & attrition_2015==0
		loc N`i'_2015 = r(N)
		count if trt==`i' & attrition_2016==0
		loc N`i'_2016 = r(N)
		count if trt==`i' & attrition_2019==0
		loc N`i'_2019 = r(N)
	}
		count if !mi(trt)
		loc N_2014 = r(N)
		count if !mi(trt) & attrition_2015==0
		loc N_2015 = r(N)
		count if !mi(trt) & attrition_2016==0
		loc N_2016 = r(N)
		count if !mi(trt) & attrition_2019==0
		loc N_2019 = r(N)
	
	mat T = J(3,6,.) 
		loc i = 0
		foreach var in attrition_2015 attrition_2016 attrition_2019 {
			g `var'2=100*(1-`var')
			loc i = `i' + 1
			tabstat `var'2, by(trt) stat(mean) save
			mat T[`i',1] = r(Stat1)
			mat T[`i',2] = r(Stat2)
			mat T[`i',3] = r(Stat3)
			mat T[`i',4] = r(Stat4)
			mat T[`i',5] = r(Stat5)
			mat T[`i',6] = r(StatTotal) 
			mat list T
			mat colnames T = C V R RV CV Total
			mat rownames T = attrition_2015 attrition_2016 attrition_2019
			mat list T
		}
	matmap T T_new, map(round(@,0.1))
	mat list T_new
	forv i = 1/3 { 
		forv j = 1/6 { 
			loc item_`i'_`j' = string(T[`i',`j'], "%15.1fc") 
		}
	} 
	cap file close
	file open fh using "$path/replication/output/Table1.tex", replace write
	file write fh " Control                   & `N1_2014' & `N1_2015' & `item_1_1'\% & `N1_2016' & `item_2_1'\% & `N1_2019' & `item_3_1'\% \\ " _n		
	file write fh " Voucher                   & `N2_2014' & `N2_2015' & `item_1_2'\% & `N2_2016' & `item_2_2'\% & `N2_2019' & `item_3_2'\% \\ " _n		
	file write fh " Recommendations           & `N3_2014' & `N3_2015' & `item_1_3'\% & `N3_2016' & `item_2_3'\% & `N3_2019' & `item_3_3'\% \\ " _n		
	file write fh " Voucher + Recommendations & `N4_2014' & `N4_2015' & `item_1_4'\% & `N4_2016' & `item_2_4'\% & `N4_2019' & `item_3_4'\% \\ " _n		
	file write fh " Control Villages          & `N5_2014' & `N5_2015' & `item_1_5'\% & `N5_2016' & `item_2_5'\% & `N5_2019' & `item_3_5'\% \\ " _n		
	file write fh " Total                     & `N_2014' & `N_2015' & `item_1_6'\% & `N_2016' & `item_2_6'\% & `N_2019' & `item_3_6'\% \\ \bottomrule  " _n		
	file close fh 

end
                  

program attrition_2016	
	
	use "$path/replication/data/attrition_balance1.dta", clear
		
	***1. Regression of attrition on treatment in 2016	
	preserve
		drop if trt==5
		reg attrition_2016 i.trt2, robust cl(vil)
			outreg2 using "$path/replication/output/TableA2.doc", replace ctitle(attrition 2016) ///
			addtext(Village FE, NO) label									
															
		xtreg attrition_2016 i.trt2, fe robust cl(vil)													
			outreg2 using "$path/replication/output/TableA2.doc", append ctitle(attrition 2016) ///
			nocons addtext(Village FE, YES) label									
	 restore
end


program attrition_2019	

	use "$path/replication/data/attrition_balance2.dta", clear
	gl outcomes fert_kg_acre_2014 fert_kg_gps_2014 fert_dum_2014 yieldkg_acre gps_mz_kg_acre_q30_2014 	
	gl controls malehead headage headeduc headeducp distplotmin credit ///
	         remittances asstfullsc livestock hhsize area_owned /// 
			 close_chairman train extension mz_acres_2014 seeds_2014 
	ta trt2, gen(trt2_)
	ta trt if attrition_2019==0
	
	la var fert_kg_acre_2014 "Fertilizer (kg/ SR acre)"                 
	la var fert_kg_gps_2014 "Fertilizer (kg/ GPS acre) "               
	la var fert_dum_2014 "Fertilizer (=1) "                         
	la var yieldkg_acre "Yields (kg/ SR acre) "                    
	la var gps_mz_kg_acre_q30_2014 "Yields (kg/ GPS acre)"                    
	la var malehead "Male-Head (=1)"                          
	la var headage "Head Age (Years) "                       
	la var headeduc "Head Education (=1 if some education) "
	la var headeducp "Head Education (=1 if beyond primary)" 
	la var distplotmin "Distance to plot in minutes"             
	la var credit "Credit Access (=1) "                     
	la var remittances "Remittances (=1) "                       
	la var asstfullsc "Asset Index "                           
	la var livestock "Livestock Ownership (=1) "              
	la var hhsize "Household Size"                          
	la var area_owned "Area Owned (SR acres) "                 
	la var close_chairman "Close to Chairman (=1) "                
	la var train "Received Training (=1) "                
	la var extension "Visited by Extension (=1)"               
	la var mz_acres_2014 "Maize Area (SR acres)"                   
	la var seeds_2014 "Improved Seeds (=1) " 
			
	***3. Regression of attrition on treatment in 2019	
	preserve
		drop if trt==5
		reg attrition_2019 i.trt2, robust cl(vil)
			outreg2 using "$path/replication/output/TableA2.doc", append ctitle(attrition 2019) ///
			addtext(Village FE, NO) label									
															
		xtreg attrition_2019 i.trt2, fe robust cl(vil)													
			outreg2 using "$path/replication/output/TableA2.doc", append ctitle(attrition 2019) ///
			nocons addtext(Village FE, YES) label									
	restore 
	erase "$path/replication/output/TableA2.txt"
end

 
program balance_reg_outcomes

	preserve 
	drop if trt==5
	***5. Regression of outcomes on treatment
	loc n : list sizeof global(outcomes) 
 	mat P1 = J(`n', 3, .) 
	loc n = 0
	foreach var in $outcomes {	
		loc n=`n'+1
		xtreg `var' i.trt2 if gps_area_old_2016!=., fe robust cl(vil)
		test 2.trt2=3.trt2
		mat P1[`n',1] = r(p)  
		test 2.trt2=4.trt2
		mat P1[`n',2] = r(p)
		test 3.trt2=4.trt2
		mat P1[`n',3] = r(p)
		mat list P1
			outreg2 using "$path/replication/output/Table2.xls", append ///
				bd(2) sd(2) nocons addtext(Village FE, YES) label
		 }
		mat rownames P1 = $outcomes
		mat colnames P1 = V_vs_R V_vs_RV R_vs_RV
		svmat P1
		mat list P1
		matmap P1 P1_new, map(round(@,0.01))
		mat list P1_new
		mat colnames P1_new = V_vs_R V_vs_RV R_vs_RV
		putexcel set "$path/replication/output/Table2_col1_5-7.xls", sheet("F-tests") replace
		putexcel B2=matrix(P1_new)
		putexcel B1="V_vs_R"
		putexcel C1="V_vs_RV"
		putexcel D1="R_vs_RV"	
		putexcel E1="Control mean"	
		loc k=1
		foreach x in $outcomes { 
			loc k = `k' + 1
			su `x' if trt2==1
			loc y_`k' = string(`r(mean)', "%15.2fc")
			putexcel A`k'="`x'"
			putexcel E`k'= "`y_`k''"
		}
	restore


	loc n : list sizeof global(outcomes) 
 	mat P1 = J(`n', 3, .) 
	loc n = 0
	foreach var in $outcomes {	
		loc n=`n'+1
		reg `var' i.trt2 if gps_area_old_2016!=., robust cl(vil)
		test 2.trt2=3.trt2
		mat P1[`n',1] = r(p)  
		test 2.trt2=4.trt2
		mat P1[`n',2] = r(p)
		test 3.trt2=4.trt2
		mat P1[`n',3] = r(p)
		mat list P1
			outreg2 using "$path/replication/output/TableA1.xls", append ///
				bd(2) sd(2) nocons addtext(Village FE, YES) label
		 }
		mat rownames P1 = $outcomes
		mat colnames P1 = V_vs_R V_vs_RV R_vs_RV
		svmat P1
		mat list P1
		matmap P1 P1_new, map(round(@,0.01))
		mat list P1_new
		mat colnames P1_new = V_vs_R V_vs_RV R_vs_RV
		putexcel set "$path/replication/output/TableA1_col1_5-7.xls", sheet("F-tests") replace
		putexcel B2=matrix(P1_new)
		putexcel B1="V_vs_R"
		putexcel C1="V_vs_RV"
		putexcel D1="R_vs_RV"	
		putexcel E1="Control mean"	
		loc k=1
		foreach x in $outcomes { 
			loc k = `k' + 1
			su `x' if trt2==1
			loc y_`k' = string(`r(mean)', "%15.2fc")
			putexcel A`k'="`x'"
			putexcel E`k'= "`y_`k''"
		}

end								


program balance_reg_covariates

	***7. Regression of controls on treatment
	preserve 
	drop if trt==5
		loc n : list sizeof global(controls)  	
		mat Z1 = J(`n', 3, .) 
		loc n = 0
		foreach var in $controls {
		loc n=`n'+1
			xtreg `var' i.trt2 if gps_area_old_2016!=., fe robust cl(vil)
			test 2.trt2=3.trt2
			mat Z1[`n',1] = r(p)  
			test 2.trt2=4.trt2
			mat Z1[`n',2] = r(p)
			test 3.trt2=4.trt2
			mat Z1[`n',3] = r(p)
			mat list Z1

			outreg2 using "$path/replication/output/Table2.xls", append ///
				bd(2) sd(2) nocons addtext(Village FE, YES) label
			 }
			mat rownames Z1 = $controls
			mat colnames Z1 = V_vs_R V_vs_RV R_vs_RV

			svmat Z1
			matmap Z1 Z1_new, map(round(@,0.01))
			mat list Z1_new
		mat colnames Z1_new = V_vs_R V_vs_RV R_vs_RV
		putexcel set "$path/replication/output/Table2_col1_5-7.xls", sheet("F-tests") modify
		putexcel B7=matrix(Z1_new)
		putexcel B1="V_vs_R"
		putexcel C1="V_vs_RV"
		putexcel D1="R_vs_RV"	
		putexcel E1="Control mean"	
		loc k=6
		foreach x in $controls { 
			loc k = `k' + 1
			su `x' if trt2==1
			loc y_`k' = string(`r(mean)', "%15.2fc")
			putexcel A`k'="`x'"
			putexcel E`k'= "`y_`k''"
		}
	restore
	
		loc n : list sizeof global(controls)  	
		mat Z1 = J(`n', 3, .) 
		loc n = 0
		foreach var in $controls {
		loc n=`n'+1
			reg `var' i.trt2 if gps_area_old_2016!=., robust cl(vil)
			test 2.trt2=3.trt2
			mat Z1[`n',1] = r(p)  
			test 2.trt2=4.trt2
			mat Z1[`n',2] = r(p)
			test 3.trt2=4.trt2
			mat Z1[`n',3] = r(p)
			mat list Z1

			outreg2 using "$path/replication/output/TableA1.xls", append ///
				bd(2) sd(2) nocons addtext(Village FE, YES) label
			 }
			mat rownames Z1 = $controls
			mat colnames Z1 = V_vs_R V_vs_RV R_vs_RV

			svmat Z1
			matmap Z1 Z1_new, map(round(@,0.01))
			mat list Z1_new
			putexcel set "$path/replication/output/TableA1_col1_5-7.xls", sheet("F-tests") modify
			putexcel B7=matrix(Z1_new)
			putexcel B1="V_vs_R"
			putexcel C1="V_vs_RV"
			putexcel D1="R_vs_RV"	
			putexcel E1="Control mean"	
			loc k=6
			foreach x in $controls { 
				loc k = `k' + 1
				su `x' if trt2==1
				loc y_`k' = string(`r(mean)', "%15.2fc")
				putexcel A`k'="`x'"
				putexcel E`k'= "`y_`k''"
			}
	erase "$path/replication/output/TableA1.txt"
	erase "$path/replication/output/Table2.txt"

end


*Execute
main

*EOF		
			

